package demo4.Controller;

import demo4.mapper.IUserMapper;
import demo4.mapper.UserMapper;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

/**
 * Controller：控制器
 */
@Component
public class UserController {
    @Autowired
    private IUserMapper userMapper;
    public void start(){
        System.out.println("请选择对应的操作，输入序号：");
        System.out.println("1.注册，2：登录，3：修改用户信息 4：用户列表 5：删除 6：**");
        Scanner sc=new Scanner(System.in);
        int num=Integer.parseInt(sc.nextLine());
        switch (num){
            case 1:
                regUser();
                break;
            case 2:
                loginUser();
                break;
            case 3:
                updataUser();
                break;
            case 4:
                    listUser();
                    break;
            case 5:
                deleteUser();
                break;
            default:
                System.out.println("输入有误");
        }
    }
    public void deleteUser(){
        System.out.println("开始处理用户删除");
      Scanner scan=new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username=scan.nextLine();

        if(userMapper.selectByUsername(username)==null){
            System.out.println("用户不存在");
        }else {
            int num=userMapper.deleteByUsername(username);
            System.out.println(num>0?"删除成功":"删除失败");
        }

    }
    public void listUser(){
        System.out.println("列出所有用户");
        List<User> userList =userMapper.selectAll();
        for (User user:userList){
            System.out.println(user);
        }
    }
    public void updataUser(){
        System.out.println("开始处理用户修改");
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username=sc.nextLine();
        System.out.println("请输入新密码：");
        String password=sc.nextLine();
        System.out.println("请输入新昵称：");
        String nickname=sc.nextLine();
        System.out.println("请输入年龄：");
        int age=sc.nextInt();
        if(userMapper.selectByUsername(username)==null){
            System.out.println("用户不存在");
        }else {
            User user=new User(null,username,password,nickname,age);
            int num=userMapper.updata(user);
            System.out.println(num>0?"修改成功":"查无此人");
        }
    }
    public void regUser() {
        System.out.println("开始处理用户注册");
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username=sc.nextLine();
        System.out.println("请输入密码：");
        String password=sc.nextLine();
        System.out.println("请输入昵称：");
        String nickname=sc.nextLine();
        System.out.println("请输入年龄：");
        int age=sc.nextInt();
        if (userMapper.selectByUsername(username)!=null){
            System.out.println("用户名已存在");
        }else {
            User user = new User(null, username, password, nickname, age);
            //User user=InputUtil.getInputObject(new User());
            int num = userMapper.insert(user);
            System.out.println(num > 0 ? "注册成功" : "注册失败");
        }
    }

    public void loginUser() {

        System.out.println("开始处理用户登录");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.nextLine();
        System.out.println("请输入密码：");
        String password = sc.nextLine();
        User user = userMapper.selectByUsername(username);
        if (user == null) {
            System.out.println("用户不存在");
        } else if (user.getPassword().equals(password)) {
            System.out.println("登录成功");
        } else {
            System.out.println("密码错误");
        }
    }
}
